package com.tnovoselec.android.mojevijesti.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nth.android.image.ImageDownloader;
import com.tnovoselec.android.mojevijesti.R;
import com.tnovoselec.android.mojevijesti.dto.ChannelsResultHolder;

public class ChannelsAdapter extends BaseAdapter{
	
	private ImageDownloader imageDownloader;
	private LayoutInflater inflater;
	private Context mContext;
	private int activeChannelPosition = 0;

	public ChannelsAdapter(Context mContext){
		inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		imageDownloader = ImageDownloader.getInstance();
		this.mContext = mContext;
	}
	
	@Override
	public int getCount() {
		return ChannelsResultHolder.getAll().size();
	}

	@Override
	public Object getItem(int position) {
		return ChannelsResultHolder.getAll().get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null){
			convertView = inflater.inflate(R.layout.channel_item, parent, false);
			holder = new ViewHolder();
			holder.logo = (ImageView) convertView.findViewById(R.id.channel_logo);
			holder.name = (TextView)convertView.findViewById(R.id.channel_name);
			convertView.setTag(holder);
		}
		holder = (ViewHolder) convertView.getTag();
		holder.name.setText(ChannelsResultHolder.getAll().get(position).getChannelName());
		imageDownloader.download(ChannelsResultHolder.getAll().get(position).getIconUrl(), holder.logo, true);
		if (position == activeChannelPosition){
			convertView.setBackgroundColor(mContext.getResources().getColor(android.R.color.transparent));
		//	holder.name.setTextColor(Color.BLACK);
		} else {
			convertView.setBackgroundColor(mContext.getResources().getColor(android.R.color.black));
		//	holder.name.setTextColor(Color.WHITE);
		}
		return convertView;
	}
	
	class ViewHolder{
		ImageView logo;
		TextView name;
	}
	public void setActiveChannel(int position){
		this.activeChannelPosition = position;
		notifyDataSetChanged();
	}

}
